//
//  HMEmoticonToolBar.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/16.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit


/// 表情类型
///
/// - Recent:  最近
/// - Default: 默认
/// - Emoji:   Emoji description
/// - Lxh:     浪小花
enum HMEmotionType: Int {
    case Recent = 0, Default, Emoji, Lxh, Mxn
}

protocol HMEmoticonToolBarDelegate : NSObjectProtocol{
    
    // 抛出方法
    func switchEmojBtn(target:HMEmoticonToolBar,type:HMEmotionType)
}


class HMEmoticonToolBar: UIStackView {
    
    // 记录上一个按钮
    var lastBtn: UIButton = UIButton()
    
    var delegate: HMEmoticonToolBarDelegate?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 选中section对应的按钮
    ///
    /// - parameter section: <#section description#>
    func selectedButtonWithSection(section: Int) {
        
        // 1. 取到section对应的按钮
        let button = viewWithTag(section)! as! UIButton
        
        if button == lastBtn {
            return
        }
        
        // 2. 选中该按钮
        button.selected = true
        // 3. 让之前的按钮取消选中
        lastBtn.selected = false
        // 4. 记录当前选中的按钮,供下次使用
        lastBtn = button
    }
    
    
    private func setupUI() {
        
        tag = 1000
        
        distribution = .FillEqually
        
        addChildButton("最近", bgImageNamed: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", bgImageNamed: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", bgImageNamed: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgImageNamed: "compose_emotion_table_right", type: .Lxh)
        addChildButton("喵小妮", bgImageNamed: "compose_emotion_table_right", type: .Mxn)

    }
    
    private func addChildButton(title: String, bgImageNamed: String, type: HMEmotionType) {
        
        let button = UIButton()
        
        // 给按钮添加点击事件
        button.addTarget(self, action: #selector(HMEmoticonToolBar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        // 设置按钮的tag,以便区分哪个按钮点击了
        button.tag = type.rawValue
        
        // 设置文字以及不同状态下的背景图片
        button.setTitle(title, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_selected"), forState: .Selected)
        
        // 设置字体大小和不同状态下的文字颜色
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        
        // 添加控件
        addArrangedSubview(button)
    }

    
    @objc func buttonClick(btn:UIButton) {
        
        // 单击后，当前按钮被选中，上一个按钮清楚
        btn.selected = true
        lastBtn.selected = false
        lastBtn = btn
        
        // 把事件抛出去，给keyboardView，用代理
        delegate?.switchEmojBtn(self, type: HMEmotionType(rawValue: btn.tag)!)
    }
    
    

}
